// 1. Core Layer - 任务的核心逻辑
class Task {
    constructor(name, action) {
        this.name = name;
        this.action = action;
        this.completed = false;
    }

    execute() {
        console.log(`Executing task: ${this.name}`);
        this.action();
        this.completed = true;
        console.log(`Task ${this.name} completed`);
    }
}

// 2. Application Layer - 任务管理与调度
class TaskScheduler {
    constructor() {
        this.tasks = [];
    }

    addTask(task) {
        this.tasks.push(task);
    }

    // 顺序执行任务
    executeSequentially() {
        console.log('Executing tasks sequentially...');
        for (let task of this.tasks) {
            if (!task.completed) {
                task.execute();
            }
        }
    }

    // 区间执行任务（执行特定范围内的任务）
    executeInRange(startIndex, endIndex) {
        console.log(`Executing tasks from index ${startIndex} to ${endIndex}...`);
        for (let i = startIndex; i <= endIndex; i++) {
            const task = this.tasks[i];
            if (task && !task.completed) {
                task.execute();
            }
        }
    }

    // 设置断点
    setBreakpoint(index) {
        console.log(`Setting breakpoint at task index: ${index}`);
        for (let i = index; i < this.tasks.length; i++) {
            const task = this.tasks[i];
            if (task && !task.completed) {
                console.log(`Task ${task.name} paused at breakpoint.`);
                return;  // 暂停在此任务
            }
        }
    }
}

// 3. Infrastructure Layer - 各种任务实现
class CommandTask extends Task {
    constructor() {
        super('Command Task', () => {
            console.log('Sending command...');
        });
    }
}

class MeasurementTask extends Task {
    constructor() {
        super('Measurement Task', () => {
            console.log('Measuring telemetry data...');
        });
    }
}

class SnapshotTask extends Task {
    constructor() {
        super('Snapshot Task', () => {
            console.log('Taking snapshot...');
        });
    }
}

class TimerTask extends Task {
    constructor() {
        super('Timer Task', () => {
            console.log('Executing timer-related task...');
        });
    }
}

// 4. UI Layer (模拟用户交互)
const scheduler = new TaskScheduler();

// 创建不同类型的任务
const commandTask = new CommandTask();
const measurementTask = new MeasurementTask();
const snapshotTask = new SnapshotTask();
const timerTask = new TimerTask();

// 添加任务到调度器
scheduler.addTask(commandTask);
scheduler.addTask(measurementTask);
scheduler.addTask(snapshotTask);
scheduler.addTask(timerTask);

// 执行任务
scheduler.executeSequentially();

// 设置断点
scheduler.setBreakpoint(2);

// 区间执行任务
scheduler.executeInRange(1, 3);
